// Copyright 2009 Pigasus.
// GNU GPL v2.
// Author: Jordan Rupprecht.

#include "MiniCppUnit-2.5/MiniCppUnit.hxx"

#include <string>

#include "../src/pigasus-types.h"
#include "../src/serialization.h"

class SerializationTest : public TestFixture<SerializationTest> {
 private:

 public:
  TEST_FIXTURE(SerializationTest) {
    TEST_CASE(TestString);
    TEST_CASE(TestInt);
    TEST_CASE(TestFloat);
    TEST_CASE(TestList);
    TEST_CASE(TestMap);
    TEST_CASE(TestFile);
  }
  
  void TestString() {
    string x = "a b c d e";
    string serialized = Serialize(x);
    string unserialized = UnserializeString(serialized);
    ASSERT_EQUALS(x, unserialized);
  }
  
  void TestInt() {
    int x = -42;
    string serialized = Serialize(x);
    int unserialized = UnserializeInt(serialized);
    ASSERT_EQUALS(x, unserialized);
  }
  
  void TestFloat() {
    double x = 512.0f;
    string serialized = Serialize(x);
    double unserialized = UnserializeFloat(serialized);
    ASSERT_EQUALS(x, unserialized);
  }
  
  void TestList() {
    List x;
    Map m;
    
    m.Set("a", "b");
    
    x.Append(5);
    x.Append("hello world\n");
    x.Append(12.61);
    x.Append(m);
    string serialized = Serialize(x);
    List *unserialized = UnserializeList(serialized);
    ASSERT_EQUALS(*((int*)unserialized->Get(0)), 5);
    ASSERT_EQUALS(*((string*)unserialized->Get(1)), "hello world\n");
    ASSERT_EQUALS(*((double*)unserialized->Get(2)), 12.61);
    ASSERT_EQUALS(*((string*)((Map*)unserialized->Get(3))->Get("a")), "b");
    delete unserialized;
  }
  
  void TestMap() {
    Map x;
    List a;
    
    a.Append(42);
    a.Append("abc123");
    
    x.Set("x", "y");
    x.Set("z", 12);
    x.Set("w", a);
    
    string serialized = Serialize(x);
    Map *unserialized = UnserializeMap(serialized);
    ASSERT_EQUALS(*((string*)unserialized->Get("x")), "y");
    ASSERT_EQUALS(*((int*)unserialized->Get("z")), 12);
    ASSERT_EQUALS(*((int*)((List*)unserialized->Get("w"))->Get(0)), 42);
    ASSERT_EQUALS(*((string*)((List*)unserialized->Get("w"))->Get(1)), "abc123");
    delete unserialized;
  }
  
  void TestFile() {
    File x("abc.txt");
    string serialized = Serialize(x);
    File *unserialized = UnserializeFile(serialized);
    ASSERT_EQUALS(unserialized->Filename(), "abc.txt");
    delete unserialized;
  }
};

REGISTER_FIXTURE(SerializationTest);
